package com.cg.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDateTime;
//出现405是方法不允许
//500为服务器出现错误
//404为未找到该路径
//200正常访问

@WebServlet("/login")
public class LoginSelvet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //利用函数获取参数值
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        if("123".equals(username)&&"123456".equals(password)){
//            //cookie，服务器保存在浏览器上，存储时间少，4kb
//            Cookie cookie1 = new Cookie("username",username);
//            Cookie cookie2 = new Cookie("loginTime", LocalDateTime.now().toString());
//            resp.addCookie(cookie1);
//            resp.addCookie(cookie2);
            //获取当前会话对象
            HttpSession session = req.getSession();
            session.setAttribute("username",username);
            session.setAttribute("longTime",LocalDateTime.now().toString());

            resp.sendRedirect(req.getContextPath()+"/success.html");//跳转路径
        }else {
            resp.sendRedirect(req.getContextPath()+"/fail.html");
        }
//        //设置响应内容类型
//        resp.setContentType("text/html;charset=utf-8");
//        //设置打印信息
//        PrintWriter writer = resp.getWriter();
//        writer.println("<h1>接收到的信息：</h1>");
//        writer.println("用户名："+username);
//        writer.println("密码："+password);


//        System.out.println("上下文访问路径"+req.getContextPath());//获取上下文访问路径
    }
}
